﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace NETXPF.Library
{
    /// <summary>
    /// Thread-safe general purpose logging class with simple code-driven configuration.
    /// 
    /// Two easy steps to use:
    /// 1. Set 'File' property to path of log file.
    /// 2. Begin calling Log.* methods to log events.
    /// </summary>
    public static class Log
    {
        private static object SyncRoot { get { return typeof(Log); } }
        public static string File { get; set; }
        public static void Exception(Exception x)
        {
            if (string.IsNullOrEmpty(File)) return; //prevent exceptions
            lock (SyncRoot)
            {
                using (StreamWriter sw = new StreamWriter(File, true))
                {
                    sw.WriteLine(DateTime.Now.ToString());
                    sw.WriteLine(x.ToString());
                    sw.WriteLine("--");
                }
            }
        }
        public static void Message(string msg)
        {
            if (string.IsNullOrEmpty(File)) return; //prevent exceptions
            lock (SyncRoot)
            {
                using (StreamWriter sw = new StreamWriter(File, true))
                {
                    sw.WriteLine(DateTime.Now.ToString());
                    sw.WriteLine(msg);
                    sw.WriteLine("--");
                }
            }
        }
    }
}
